package com.power200.showcase;

import android.app.ListActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SimpleListActivity extends AppCompatActivity {

    private List<Map<String,String>> textInfo ;
    private ListView listView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_list);

        System.out.println("on Create ");
        listView = new ListView(this);
        listView = (ListView) findViewById(R.id.simpleListView);
        listView.setAdapter(new SimpleAdapter(
                this,getData(), R.layout.simplelistviewitem, new String[]{"itemTitle","itemItem"},new int[]{R.id.itemTitle,R.id.itemItem}));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    // 或者采用此方式
    protected void onCreate2(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        System.out.println("on Create ");
        listView = new ListView(this);
        listView.setAdapter(new SimpleAdapter(
                this,getData(), R.layout.simplelistviewitem, new String[]{"itemTitle","itemItem"},new int[]{R.id.itemTitle,R.id.itemItem}));
        setContentView(listView);
    }

    private List<Map<String,String>> getData() {
        if(textInfo == null) {
            textInfo = new ArrayList<Map<String,String>>();
            for (int i = 0 ; i < 10 ;i++) {
                Map<String,String> each = new HashMap<String,String>();
                each.put("itemTitle","itemTitle"+i);
                each.put("itemItem","itemItem"+i);
                textInfo.add(each);
            }
        }
        return  textInfo;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_simple_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
